package com.ctcemti.mdm.common.result;

import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.common.util.ServletUtils;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpServletRequest;


public class Result<T> {

	/**
     * 错误码
     */
    private int code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 具体数据内容
     */
    private T data;
    /**
     * 当前时间
     */
    private String now;
    /**
     * Refresh的token
     */
    private String refreshToken;

    public Result() {

    }

    public Result(int i, String message, Object object) {
		this.code = i;
		this.msg = message;
		this.data = (T) object;
		this.now = String.valueOf(System.currentTimeMillis());
	}

    public Result(ResultEnum resultEnum, Object object) {
        HttpServletRequest request = ServletUtils.getRequest();
        String token = request.getHeader("Authorization");
        if(!StringUtil.isEmpty(token) && !JWTUtil.checkToken(token)){
            this.refreshToken = JWTUtil.reGenerateToken(token);
        }
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
        this.data = (T) object;
        this.now = String.valueOf(System.currentTimeMillis());
    }

    public Result(int i, String message, Object object,String now) {
        this.code = i;
        this.msg = message;
        this.data = (T) object;
        this.now = now;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getNow() {
        return now;
    }

    public void setNow(String now) {
        this.now = now;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if(!StringUtil.isEmpty(token) && !JWTUtil.checkToken(token)){
            this.refreshToken = JWTUtil.reGenerateToken(token);
        }
    }
}
